package com.zhuozhou.jdyc.wight.dialog;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.util.FingerLock;
import com.zhuozhou.jdyc.util.log.ToastUtils;

public class FingerDialog extends Dialog implements View.OnClickListener {
    private Context context;
    private Dialog dialog;
    private View inflate;
    private TextView dialog_sure, verification_msg;
    private FingerBack fingerBack;
    private FingerLock f;

    public FingerDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }


    public void create() {
        dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        // 填充对话框的布局
        inflate = LayoutInflater.from(context).inflate(
                R.layout.diaiog_fingerlock, null);
        // 初始化控件
        dialog_sure = inflate.findViewById(R.id.fingerprint_cancel_tv);
        verification_msg = inflate.findViewById(R.id.tv_verification_msg);
        dialog_sure.setOnClickListener(this);
        // 将布局设置给Dialog
        dialog.setContentView(inflate);
        // 获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        // 设置dialog宽度
        if (dialogWindow == null) {
            return;
        }
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        if (wm != null) {
            wm.getDefaultDisplay().getMetrics(dm);
            dialogWindow.setLayout(dm.widthPixels * 3 / 4,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        } else {
            dialogWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }

        // 设置Dialog从窗体中间弹出
        dialogWindow.setGravity(Gravity.CENTER);
        dialog.show();// 显示对话框
        f = new FingerLock(context);
        f.start(new FingerLock.FingerPrintResult() {
            @Override
            public void success() {
                dialog.hide();
                ToastUtils.showShort(context, "验证成功！");
                fingerBack.fingerBack(true);
            }

            @Override
            public void error(int code, CharSequence info) {
                verification_msg.setText(info);
                verification_msg.setVisibility(View.VISIBLE);
            }

            @Override
            public void retry(int code, CharSequence info) {
                verification_msg.setText(info);
                verification_msg.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fingerprint_cancel_tv:
                dialog.hide();
                break;
        }
    }

    @Override
    public void hide() {
        super.hide();
        if (f != null) f.cancelCallback();
    }

    public void setCallBackListener(FingerBack fingerBack) {
        this.fingerBack = fingerBack;
    }

    public interface FingerBack {
        void fingerBack(boolean FingerCorrect);
    }
}
